package com.lily.aidouzixun.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 *  文章
 *
 */
public class ArticleHomeInfo implements Parcelable {

    /**
     * json key
     */
    /**
     * 轮播信息
     */
    private static final String JSONKEY_CAROUSELINFO= "carouselInfo";
    /**
     * 文章列表
     */
    private static final String CURPAGE_JOSANKEY = "curpage";
    private static final String TOTALPAGE_JOSANKEY = "totalpage";
    private static final String JSONKEY_LONGDISTANCELIST = "longDistanceList";

    /**
     * 轮播列表
     */
    private SparseArray<ArticleInfo> carouselList;
    /**
     * 文章列表
     */
    private SparseArray<ArticleInfo> longDistanceItenList;
    private int curpage;
    private int totalpage;

    public int getCurpage() {
        return curpage;
    }

    public void setCurpage(int curpage) {
        this.curpage = curpage;
    }

    public int getTotalpage() {
        return totalpage;
    }

    public void setTotalpage(int totalpage) {
        this.totalpage = totalpage;
    }
    public SparseArray<ArticleInfo> getCarouselList() {
        return carouselList;
    }

    public void setCarouselList(SparseArray<ArticleInfo> carouselList) {
        this.carouselList = carouselList;
    }

    public SparseArray<ArticleInfo> getLongDistanceItenList() {
        return longDistanceItenList;
    }

    public void setLongDistanceItenList(SparseArray<ArticleInfo> longDistanceItenList) {
        this.longDistanceItenList = longDistanceItenList;
    }

    /**
     * 解析数据
     * @param jsonObj
     * @return 文章首页数据
     */
    public static ArticleHomeInfo parseInfo(JSONObject jsonObj) {
        if (jsonObj == null) {
            return null;
        }
        ArticleHomeInfo longDistanceHome = new ArticleHomeInfo();
        if (jsonObj.has(CURPAGE_JOSANKEY)) {
            longDistanceHome.setCurpage(jsonObj.optInt(CURPAGE_JOSANKEY));
        }
        if (jsonObj.has(TOTALPAGE_JOSANKEY)) {
            longDistanceHome.setTotalpage(jsonObj.optInt(TOTALPAGE_JOSANKEY));
        }
        if (jsonObj.has(JSONKEY_CAROUSELINFO)) {    //轮播信息
            SparseArray<ArticleInfo> carouselList = new SparseArray<ArticleInfo>();
            JSONArray carouseJsonArr = jsonObj.getJSONArray(JSONKEY_CAROUSELINFO);
            for (int i = 0; i<carouseJsonArr.size(); i++) {
                JSONObject carouselJsonObj = carouseJsonArr.getJSONObject(i);
                ArticleInfo longDistanceInfo = ArticleInfo.parseInfo(carouselJsonObj);
                if (longDistanceInfo != null) {
                    carouselList.put(i, longDistanceInfo);
                }
            }
            longDistanceHome.setCarouselList(carouselList);
        }

        if (jsonObj.has(JSONKEY_LONGDISTANCELIST)) {    //文章列表信息
            SparseArray<ArticleInfo> longdistanceList = new SparseArray<ArticleInfo>();
            JSONArray carouseJsonArr = jsonObj.getJSONArray(JSONKEY_LONGDISTANCELIST);
            for (int i = 0; i<carouseJsonArr.size(); i++) {
                JSONObject carouselJsonObj = carouseJsonArr.getJSONObject(i);
                ArticleInfo longDistanceInfo = ArticleInfo.parseInfo(carouselJsonObj);
                if (longDistanceInfo != null) {
                    longdistanceList.put(i, longDistanceInfo);
                }
            }
            longDistanceHome.setLongDistanceItenList(longdistanceList);
        }
        return longDistanceHome;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {

    }
}
